package nowcoderLinkList;

import java.util.Stack;

public class QueueAndStack {
    //42 用两个栈实现队列
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();

    public void push1(int node) {
        stack1.push(node);
    }

    public int pop1() {
        //将第一个栈中的全部元素弹入到第二个栈中，然后弹出第二个栈顶元素
        while (!stack1.isEmpty()) {
            stack2.push(stack1.pop());
        }
        int ret = stack2.pop();
        while (!stack2.isEmpty()) {
            stack1.push(stack2.pop());
        }
        return ret;
    }
    //43 包含min函数的栈  就是实现一个最小栈
    private Stack<Integer> stack = new Stack<>();
    private Stack<Integer> minStack = new Stack<>();
    public void push(int node) {
        stack.push(node);//普通栈存放了元素，同时最小栈存放普通栈的最小元素
        if (minStack.isEmpty()) {
            minStack.push(node);
        }else {
            if (node <= minStack.peek()) {//此处注意要取等号
                minStack.push(node);
            }
        }
    }
    public void pop() {
        if (!stack.isEmpty()) {
            int val = stack.pop();
            //同时维护最栈
            if (val == minStack.peek()) {
                minStack.pop();
            }
        }
        return;
    }
    public int top() {
        return stack.peek();
    }
    public int min() {
        return minStack.peek();
    }
    //44 有效括号序列
    public boolean isValid (String s) {
        // write code here
        return false;
    }
}
